<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author LYS
-->
<template>
  <BasicDrawer v-bind="$attrs" :showFooter="true" :okAuth="'accept:ance:pmAcceptance:edit'" @register="registerDrawer"
    @ok="handleSubmit" width="60%">
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
    <template #footer>
      <BpmButton v-model:bpmEntity="record" bpmEntityKey="id" formKey="Flow_PmAcceptance" completeText="提交"
        :loading="loadingRef" :auth="'accept:ance:pmAcceptance:edit'" @complete="handleSubmit" @success="handleSuccess"
        @close="closeDrawer" />
    </template>
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsAcceptAncePmAcceptanceForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { PmAcceptance, pmAcceptanceResultSave, pmAcceptanceForm } from '/@/api/accept/ance/pmAcceptance';
  import { BpmButton } from '/@/components/Bpm';
  import { pmProjectinformationTreeData } from '/@/api/proj/info/pmProjectinformation';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('accept.ance.pmAcceptance');
  const { showMessage } = useMessage();
  const record = ref < PmAcceptance > ({} as PmAcceptance);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value:  t('上传结果'),
  }));

  const loadingRef = ref(false);

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('项目名称'),
      field: 'projectno',
      fieldLabel: 'projectname',
      component: 'TreeSelect',

      componentProps: {
        api: pmProjectinformationTreeData,
        params: {},
        canSelectParent: false,
        allowClear: false,
      },
      itemProps: { validateTrigger: 'blur' },
      dynamicDisabled: true,
    },
    {
      label: t('项目编码'),
      field: 'projectno',
      component: 'Input',
      componentProps: {
        maxlength: 50,
        disabled: true,
      },
      dynamicDisabled: true,
    },
    {
      label: t('发起备注'),
      field: 'remarks',
      component: 'InputTextArea',
      componentProps: {
        maxlength: 200,
        rows: 8
      },
      colProps: { lg: 24, md: 24 },
      dynamicDisabled: true,
    },
    {
      label: t('开工通知书等图片'),
      field: 'dataMap',
      component: 'Upload',
      componentProps: {
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'PmAcceptance_create_image',
        uploadType: 'all',
        isPreview: true,
      },
      colProps: { lg: 24, md: 24 },
      dynamicDisabled: true,
      // rules: [
      //   { required: true },
      //   {
      //     validator(_rule, value) {
      //       return new Promise((resolve, reject) => {
      //         const len = !value || value['PmAcceptance_create_image__len'] || 0;
      //         if (len == 0) reject(t('请上传开工通知书等图片'));
      //         else resolve();
      //       });
      //     },
      //   },
      // ],
    },
    {
      label: t('中间验收表(EXCEL)'),
      field: 'dataMap',
      component: 'Upload',
      componentProps: {
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'PmAcceptance_midYs_file',
        uploadType: 'all',
        isPreview: true,
      },
      colProps: { lg: 24, md: 24 },
      dynamicDisabled: true,
      // rules: [
      //   { required: true },
      //   {
      //     validator(_rule, value) {
      //       return new Promise((resolve, reject) => {
      //         const len = !value || value['PmAcceptance_midYs_file__len'] || 0;
      //         if (len == 0) reject(t('请上传中间验收表'));
      //         else resolve();
      //       });
      //     },
      //   },
      // ],
    },

    {
      label: t('申请人'),
      field: 'createByName',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
      dynamicDisabled: true,
    },
    {
      label: t('申请时间'),
      field: 'createDate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
      dynamicDisabled: true,
    },
   


    {
      label: t('上传验收结果'),
      field: 'info',
      component: 'FormGroup',
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('验收人'),
      field: 'perno',
      fieldLabel: 'pername',
      component: 'ListSelect',
      componentProps: {
        checkbox: true,
        selectType: 'empUserSelect',
      },
      required: true,
    },

    {
      label: t('验收结果'),
      field: 'checkResult',
      component: 'RadioGroup',
      componentProps: {
        dictType: 'zf_check_result',
      },
      required: true,
    },
    {
      label: t('验收日期'),
      field: 'launchtime',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
      required: true,
    },
    {
      label: t('不合格描述'),
      field: 'unqualified',
      component: 'InputTextArea',
      componentProps: {
        maxlength: 200,
        rows: 8
      },
      colProps: { lg: 24, md: 24 },
      // required: true,
    },
    {
      label: t('验收图片'),
      field: 'dataMap',
      component: 'Upload',
      componentProps: {
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'PmAcceptance_result_image',
        uploadType: 'image',
      },
      colProps: { lg: 24, md: 24 },
      // rules: [
      //   { required: true },
      //   {
      //     validator(_rule, value) {
      //       return new Promise((resolve, reject) => {
      //         const len = !value || value['PmAcceptance_result_image__len'] || 0;
      //         if (len == 0) reject(t('请上传验收图片'));
      //         else resolve();
      //       });
      //     },
      //   },
      // ],
    },


   


  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 180,
    schemas: inputFormSchemas,
    baseColProps: { lg: 24, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await pmAcceptanceForm(data);
    record.value = (res.pmAcceptance || {}) as PmAcceptance;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      data.bpm = record.value.bpm; // 流程信息
      data.status = record.value.status; // 提交状态
      loadingRef.value = true;
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      // console.log('submit', params, data, record);
      const res = await pmAcceptanceResultSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      loadingRef.value = false;
      setDrawerProps({ confirmLoading: false });
    }
  }

  async function handleSuccess() {
    emit('success');
  }
</script>